package com.youedata.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.youedata.modular.entity.JhTravelInfo;
import com.youedata.modular.entity.TravelInfoImgRel;
import com.youedata.modular.mapper.JhTravelInfoMapper;
import com.youedata.modular.mapper.TravelInfoImgRelMapper;
import com.youedata.modular.model.dto.JhTravelInfoDTO;
import com.youedata.modular.service.IJhTravelInfoService;
import com.youedata.modular.service.ITravelInfoImgRelService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 吃在玩在建湖表 服务实现类
 * </p>
 *
 * @author hst
 * @date 2021-07-19
 */
@Service
@Transactional(rollbackFor = Exception.class)

public class JhTravelInfoServiceImpl extends ServiceImpl<JhTravelInfoMapper, JhTravelInfo> implements IJhTravelInfoService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private JhTravelInfoMapper jhTravelInfoMapper;
    @Autowired
    private ITravelInfoImgRelService travelInfoImgRelService;

    @Override
    public List queryNameFuzzy(Page page, String name, String type) {
        return jhTravelInfoMapper.queryNameFuzzy(page, name, type);
    }

    @Override
    public void saveSelective(JhTravelInfoDTO jhTravelInfoDTO) {
        JhTravelInfo jhTravelInfo = new JhTravelInfo();
        BeanUtils.copyProperties(jhTravelInfoDTO, jhTravelInfo);
        jhTravelInfo.setCreateUser(UserHolder.getUserId());
        jhTravelInfo.setCreateTime(LocalDateTime.now());
        jhTravelInfoMapper.insert(jhTravelInfo);

        String topImgUrl = jhTravelInfoDTO.getTopImgUrl();
        if (ToolUtil.isNotEmpty(topImgUrl)) {
            List<TravelInfoImgRel> travelInfoImgRels = Lists.newArrayList();
            String[] topImgUrls = topImgUrl.split(",");

            for (String imgUrl : topImgUrls) {
                TravelInfoImgRel travelInfoImgRel = new TravelInfoImgRel();
                travelInfoImgRel.setTravelId(jhTravelInfo.getId());
                travelInfoImgRel.setTopImgUrl(imgUrl);
                travelInfoImgRels.add(travelInfoImgRel);
            }

            travelInfoImgRelService.saveBatch(travelInfoImgRels);
        }


    }

    @Override
    public JhTravelInfo detailById(String id, String countFlag) {
        JhTravelInfo travelInfo = this.getById(id);

        if (StringUtils.equalsIgnoreCase("Y", countFlag)) {
            travelInfo.setConsume(travelInfo.getConsume() + 1);
            this.updateById(travelInfo);
        }

        List<TravelInfoImgRel> travelInfoImgRels = this.travelInfoImgRelService.list(new LambdaQueryWrapper<TravelInfoImgRel>().eq(TravelInfoImgRel::getTravelId, id));
        if (ToolUtil.isNotEmpty(travelInfoImgRels)) {
            StringBuffer urlsFull = new StringBuffer();
            List<String> topImgUrls = Lists.newArrayList();
            travelInfoImgRels.forEach(x -> {
                if (ToolUtil.isNotEmpty(x.getTopImgUrl())) {
                    urlsFull.append(x.getTopImgUrl()).append(",");
                    topImgUrls.add(x.getTopImgUrl());
                }
            });
            travelInfo.setTopImgUrl(urlsFull.toString());
            travelInfo.setTopImgUrls(topImgUrls);
        }
        return travelInfo;
    }

    @Override
    public void updateJhTravelInfo(JhTravelInfoDTO jhTravelInfoDto) {
        List<TravelInfoImgRel> travelInfoImgRelList = Lists.newArrayList();

        JhTravelInfo jhTravelInfo = new JhTravelInfo();
        BeanUtils.copyProperties(jhTravelInfoDto, jhTravelInfo);
        jhTravelInfo.setUpdateUser(UserHolder.getUserId());
        jhTravelInfo.setUpdateTime(LocalDateTime.now());
        jhTravelInfoMapper.updateById(jhTravelInfo);

        String topImgUrl = jhTravelInfoDto.getTopImgUrl();
        if (StringUtils.contains(topImgUrl, ",")) {
            String[] urls = topImgUrl.split(",");
            for (int i = 0; i < urls.length; i++) {
                TravelInfoImgRel travelInfoImgRel = new TravelInfoImgRel();
                travelInfoImgRel.setTravelId(jhTravelInfo.getId());
                travelInfoImgRel.setTopImgUrl(urls[i]);

                travelInfoImgRelList.add(travelInfoImgRel);
            }
            this.travelInfoImgRelService.remove(new LambdaQueryWrapper<TravelInfoImgRel>().eq(TravelInfoImgRel::getTravelId, jhTravelInfo.getId()));
            this.travelInfoImgRelService.saveBatch(travelInfoImgRelList);
        }
    }

}

